package xyz.zcq.zcqrpc.server.tcp;

import io.vertx.core.Vertx;
import io.vertx.core.net.NetServer;
import xyz.zcq.zcqrpc.server.HttpServer;

/**
 * Vertx Tcp 服务器
 */
public class VertxTcpServer implements HttpServer {

    @Override
    public void doStart(int port) {
        // 创建Tcp服务器实例
        NetServer netServer = Vertx.vertx().createNetServer();
        // 请求处理
        netServer.connectHandler(new TcpServerHandler())
                .listen(port, asyncResult -> {
                    if (asyncResult.succeeded()) {
                        System.out.println("Tcp server is now listening on port " + port);
                    } else {
                        System.err.println("Failed to start tcp server: " + asyncResult.cause());
                    }
                });
        // 指定监听端口，启动服务器
    }

    /**
     * 请求处理
     *
     * @param reqData 请求数据
     * @return 响应数据
     */
    private byte[] handleRequest(byte[] reqData) {
        return "Hello, client!".getBytes();
    }

    public static void main(String[] args) {
        new VertxTcpServer().doStart(8888);
    }

}
